package tr.edu.yildiz.ce.app_manager.ui.components.table;

import java.util.List;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Table;

import tr.edu.yildiz.ce.app_manager.model.Application;
import tr.edu.yildiz.ce.app_manager.ui.AppManagerApplication;
import tr.edu.yildiz.ce.app_manager.ui.action.table.ApplicationTableSelectAction;

public class ApplicationTableForm extends BasicTableForm<Application>{

	public ApplicationTableForm(AppManagerApplication app_) {
		super( app_);
	}

	@Override
	public Application getModel() {
		 return new Application();
	}
	
	@Override
	public Table getTable() {
		Table table = new Table();
		List<Application> list = app.getServiceProvider().getApplicationService().listAll();
		
		table.addContainerProperty("Application Name", String.class, null);
		table.addContainerProperty("Application Version", String.class, null);
		for (Application application : list) {
			table.addItem(new Object[]{	application.getApplicationName(),application.getApplicationVersion()},application.getApplicationId());
			
		}
		table.addListener(new ItemClickListener() {
			@Override
			public void itemClick(ItemClickEvent event) {
				ApplicationTableSelectAction appAction = new ApplicationTableSelectAction(app,(Long) event.getItemId());
				appAction.doAction();
				selectedObject = appAction.getApplication();
			}
		});
		table.setSelectable(true);
		return table;
	}

}
